from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class StatusProduct(models.Model):
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765)   
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description} 
    class Meta:
        db_table = 'StatusProduct'
        
class StatusComponent(models.Model):
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765)    
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'StatusComponent'
    

class Supplier(models.Model):
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(max_length=765)
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'Supplier'
    
    

     

        
class Category(models.Model):    
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765) 
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'Category'



class TemplateComponent(models.Model):
    img = models.ImageField(upload_to="img", width_field="250", blank=True, null=True)
    category = models.ForeignKey(Category) # 
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765)
    supplier = models.ForeignKey(Supplier)
    warranty_time = models.IntegerField(max_length=1)
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'TemplateComponent'
    

class TemplateProduct(models.Model):
    img = models.ImageField(upload_to="img", width_field="250", blank=True, null=True)
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765)
    category = models.ForeignKey(Category)
    warranty_time = models.IntegerField(max_length=1)
    power = models.CharField(max_length=16, blank=True)
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'TemplateProduct'
    
        
class Language(models.Model):
    name = models.CharField(unique=True, max_length=255)
    def __unicode__(self):
        return u'%(name)s'% {"name":self.name}
    class Meta:
        db_table = 'Language'
    
    
class Country(models.Model):
    name = models.CharField(unique=True, max_length=255)
    def __unicode__(self):
        return u'%(name)s'% {"name":self.name}
    class Meta:
        db_table = 'Country'
    
        
class Company(models.Model):    
    name = models.CharField(unique=True, max_length=255)
    campus = models.CharField(max_length=255, blank=True)
    site = models.URLField("Website", blank=True, max_length=255)
    address = models.TextField(max_length=765)
    zip = models.CharField(max_length=100)
    town = models.CharField(max_length=255)
    language = models.ForeignKey(Language)
    country = models.ForeignKey(Country)
    distance = models.CharField(blank=True, max_length=100)    
    def __unicode__(self):
        return u'%(name)s %(site)s'% {"name":self.name, "site":self.site}
    class Meta:
       db_table = 'Company'
    

class Lot(models.Model):
	lot = models.CharField(max_length=4, primary_key=True)
	description = models.CharField(max_length=255)
	def __unicode__(self):
	    return u'%(lot)s '% {"lot":self.lot}


class LocationAtOwner(models.Model):
	owner = models.ForeignKey(Company)
	description = models.TextField(blank=True, max_length=255)
	def __unicode__(self):
	    return u'%(desc)s '% {"desc":self.description}

class Product(models.Model):
    serialnumber = models.CharField(unique=True, max_length=255)
    lot = models.ForeignKey(Lot, blank=True, null=True)
    name = models.ForeignKey(TemplateProduct)    
    description = models.TextField(blank=True, max_length=255)
    version = models.CharField(blank=True, max_length=255)
    installdate = models.DateTimeField(null=True, blank=True)
    purchase_date = models.DateTimeField(null=True, blank=True)    
    production_date = models.DateTimeField(null=True, blank=True)
    owner = models.ForeignKey(Company, null=True, blank=True)
    location_at_owner = models.ForeignKey(LocationAtOwner, null=True, blank=True)
    status = models.ForeignKey(StatusProduct)
    delivery_note = models.CharField(blank=True, max_length=255)
    purchase_order = models.CharField(blank=True, max_length=255)
    future_location = models.CharField(blank=True, max_length=255)    	
    sale_price = models.DecimalField(max_digits=19, decimal_places=2, blank=False, default=0) # nieuw !!!
    def __unicode__(self):
        return u'%(serialnumber)s %(name)s %(desc)s'% {"serialnumber":self.serialnumber, "name":self.name, "desc":self.description}
    class Meta:
       db_table = 'Product'




class ActionProduct(models.Model):
    location = models.ForeignKey(Company)
    product = models.ForeignKey(Product)    
    date = models.DateTimeField(null=True, blank=True)
    description = models.TextField(blank=True, max_length=765)  
    def __unicode__(self):
        return u'%(date)s  %(desc)s'% { "desc":self.description, "date":self.date}
    class Meta:
        db_table = 'ActionProduct'
    
class Component(models.Model):
    serialnumber = models.CharField(unique=True, max_length=255)
    psn = models.ForeignKey(Product) # dat moet aangepast worden
    name = models.ForeignKey(TemplateComponent)
    description = models.TextField(blank=True, max_length=765)
    date_of_purchase = models.DateTimeField(null=False)    
    software_version = models.CharField(blank=True, max_length=255)
    software_upgrade = models.CharField(blank=True, max_length=255)
    delivery_note = models.CharField(blank=False, max_length=255)
    mac_address = models.CharField(blank=True, max_length=12)
    status = models.ForeignKey(StatusComponent) # broken, working, repair
    date_status_change = models.DateTimeField(null=True, blank=True)
    mac_address2 = models.CharField(blank=True, max_length=12)    	
    purchase_price = models.DecimalField(max_digits=19, decimal_places=2, blank=False, default=0) # Nieuw !!!

    
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}        
    class Meta:
        db_table = 'Component'
        
class TemplateAction(models.Model):
    name = models.CharField(unique=True, max_length=255)
    description = models.TextField(blank=True, max_length=765)    
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}
    class Meta:
        db_table = 'TemplateAction'
        
class Action(models.Model):
    location = models.ForeignKey(Company)
    component = models.ForeignKey(Component)
    template = models.ForeignKey(TemplateAction)
    date = models.DateTimeField(null=True, blank=True)
    description = models.TextField(blank=True, max_length=765) 
    user = models.ForeignKey(User, blank=True, null=True)    
    def __unicode__(self):
        return u'%(date)s  %(desc)s'% { "desc":self.description, "date":self.date}
    class Meta:
        db_table = 'Action'





class StockComponent(models.Model):
    referention = models.CharField(unique=True, max_length=255)
    lot = models.CharField(blank=True, max_length=4)
    name = models.ForeignKey(TemplateComponent)
    description = models.TextField(blank=True, max_length=765)
    date_of_purchase = models.DateTimeField(null=False)      
    stock_location = models.CharField(blank=True, max_length=255) 
    purchase_price = models.DecimalField(max_digits=19, decimal_places=2, blank=False, default=0) # Nieuw !!!
        
    def __unicode__(self):
        return u'%(name)s %(desc)s'% {"name":self.name, "desc":self.description}        
    class Meta:
        db_table = 'StockComponent'

    
ACTION_TYPE = (
    ('IN', 'IN Stock'),
    ('OUT', 'OUT Stock')    
    )

class ActionStockComponent(models.Model):
    componet = models.ForeignKey(StockComponent)
    action_type = models.CharField(choices=ACTION_TYPE, max_length=3)    	
    number = models.PositiveIntegerField()
    description = models.TextField(blank=True, max_length=765)

    def __unicode__(self):
        return u'%(number)s %(desc)s'% {"number":self.number, "desc":self.description}        
    class Meta:
        db_table = 'ActionStockComponent'
        
PRIORITY = (
           ('Very High','Very High'), 
           ('High','High'), 
           ('Normal','Normal'), 
           ('Low','Low'), 
           ('Very Low','Very Low'), 
            )

STATUS = (
          ('Complete','Complete'),
          ('Incomplete','Incomplete')
          )
           

class TaskCategory(models.Model):
    name = models.CharField(max_length=60, unique=True)
    description = models.CharField(max_length=120, blank=True, null=True)
    def __unicode__(self):
        return u' %(name)s ' % {'name':self.name } 


class Task(models.Model):
    category = models.ForeignKey(TaskCategory)
    name = models.CharField(max_length=120, default="New task")
    priority = models.CharField(max_length=30, choices=PRIORITY, default='Normal')
    status = models.CharField(max_length=30, choices=STATUS, default='Incomplete')
    created = models.DateTimeField(auto_now_add=True)
    completed = models.DateTimeField(blank=True, null=True)
    scheduled_for = models.DateTimeField(auto_now=True, null=True)
    estimated_hour = models.IntegerField(default=1, null=True)
    actual_work_hour = models.IntegerField(default=0, null=True)
    note = models.TextField(blank=True, null=True)        
    assigned_to = models.ForeignKey(User)    
    def __unicode__(self):
        return u'%(priority)s %(name)s %(scheduled_for)s'% {"name":self.name, "priority":self.priority, "scheduled_for":self.scheduled_for}    
    class Meta:
        unique_together = (('category', 'name'),)
        get_latest_by = "created"
        ordering = ['-created']
        
        
    
    
        

    
    
